/*
 * SymbolTable.cpp
 *
 *  Created on: Nov 23, 2010
 *      Author: dhovemey
 */

#include <cassert>
#include "util/Error.h"
#include "fe/ASTNode.h"
#include "fe/SemanticAnalysisException.h"
#include "fe/SymbolTable.h"

SymbolTableEntry::SymbolTableEntry(const std::wstring &name, Kind kind)
	: m_name(name)
	, m_kind(kind)
{

}

SymbolTableEntry::~SymbolTableEntry()
{

}

SymbolTable::SymbolTable(SymbolTable *parent)
	: m_parent(parent)
{

}

SymbolTable::~SymbolTable()
{

}

SymbolTableEntry *SymbolTable::addMember(ASTNode *member)
{
	const std::wstring &name = member->getStringValue();

	// Already defined?
	Environment::iterator i = m_env.find(name);
	if (i != m_env.end()) {
		Error::raise(new SemanticAnalysisException(L"Name `" + name + L"' already defined", member->getLocation()));
		return 0;
	}

	// Figure out what kind of entry it is
	SymbolTableEntry::Kind kind;
	switch (member->getType()) {
	case ASTNode::AST_FIELD:
		kind = SymbolTableEntry::FIELD;
		break;
	case ASTNode::AST_METHOD:
	case ASTNode::AST_MAGIC_METHOD:
		kind = SymbolTableEntry::METHOD;
		break;
	default:
		assert(false);
	}

	// Add entry
	SymbolTableEntry *entry = new SymbolTableEntry(name, kind);
	m_env[name] = entry;

	return entry;
}
